from odoo import models 
from odoo import _, fields, models
from odoo.exceptions import UserError

def process_xls(sheet,records,from_main=True,user=None):
    header = ['实际数据(2位)','实际数据(3位)','精度','机型','机号','工程编号','|节点|','|工位|','|目标值|','|最小值|','|最大值|','|单位|','|所用量具|','判断检测','自定义输入','|检测类型|','|操作员|','组织','|工序要求|','工序明细 / 数据库 ID','操作员 / 数据库 ID','所用工具 / 数据库 ID','工序单号 / 数据库 ID']
    for col in range(len(header)):
        sheet.write(0, col, header[col])
        # sheet.write(0, 0, obj.name, bold)
    for row in range(1, len(records)+1):
        main_sub_id = records[row-1]
        sheet.write(row, 0, 0.00)
        sheet.write(row, 1, 0.000)
        sheet.write(row, 2, '3位精度' if main_sub_id.decimal else '2位精度')
        sheet.write(row, 3, main_sub_id.prd_name)
        sheet.write(row, 4, main_sub_id.prd_id.name)
        sheet.write(row, 5, main_sub_id.prd_num)
        sheet.write(row, 6, main_sub_id.number)
        sheet.write(row, 7, main_sub_id.position_id.name)
        sheet.write(row, 8, main_sub_id.target_value if main_sub_id.decimal else main_sub_id.target_value2)
        sheet.write(row, 9, main_sub_id.min_value if main_sub_id.decimal else main_sub_id.min_value2)
        sheet.write(row, 10, main_sub_id.max_value if main_sub_id.decimal else main_sub_id.max_value2)
        sheet.write(row, 11, main_sub_id.value_unit)
        sheet.write(row, 12, main_sub_id.tool_id.name)
        sheet.write(row, 13, main_sub_id.check)
        sheet.write(row, 14, '')
        sheet.write(row, 15, main_sub_id.worktype_value)
        # 当操作者不存在时，就选择当前登录的用户作为操作者导出
        sheet.write(row, 16, main_sub_id.operator_id.name or user.name)
        sheet.write(row, 17, main_sub_id.company_id.name)
        sheet.write(row, 18, main_sub_id.require)
        # sheet.write(row, 19, main_sub_id.meas_id.title)

        # 假如不是来自于meas.main.sub则就是来自于meas.work.sub,这时的main_sub_id需要变化
        sheet.write(row, 19, main_sub_id.id if from_main else main_sub_id.meas_main_sub_id.id)
        # 当操作者不存在时，就选择当前登录的用户作为操作者导出
        sheet.write(row, 20, main_sub_id.operator_id.id or user.id)
        sheet.write(row, 21, main_sub_id.tool_id.id)
        sheet.write(row, 22, main_sub_id.meas_main_id.id)

# class main_subXlsx(models.AbstractModel):
#     _name = 'report.meas.report_meas_main_sub_xlsx'
#     _inherit = 'report.report_xlsx.abstract'
#     # 这里是工步设置及工步节点导出excel,这时不论选择或不选择记录，都只导出已经下发的所有数据
#     def generate_xlsx_report(self, workbook,data, obj):
#         main_sub=self.env['meas.main.sub'].sudo().search([('dispatch','=',True),('company_id','=',self.env.company.id)])
#         sheet = workbook.add_worksheet('sheet1')
#         # bold = workbook.add_format({'bold': True})
#         process_xls(sheet,main_sub,user=self.env.user)

# class work_subXlsx(models.AbstractModel):
#     _name = 'report.meas.report_meas_work_sub_xlsx'
#     _inherit = 'report.report_xlsx.abstract'
#     # 这里是在工单模型中导出excel,这时只导出选中的（在工单中一定是下发的）数据。
#     def generate_xlsx_report(self, workbook,data, obj):
#         sheet = workbook.add_worksheet('sheet1')
#         # bold = workbook.add_format({'bold': True})
#         process_xls(sheet,obj,False,self.env.user)
    
class work_mainXlsx(models.AbstractModel):
    _name = 'report.meas.report_meas_work_main_xlsx'
    _inherit = 'report.report_xlsx.abstract'
    # 这里是在工单模型中导出excel,这时只导出选中的（在工单中一定是下发的）数据。
    # obj=meas.work.main
    def generate_xlsx_report(self, workbook,data, obj):
        sheet = workbook.add_worksheet('sheet1')
        # bold = workbook.add_format({'bold': True})
        data=self.env['meas.work.sub']
        for rec in obj:
            data=data+rec.work_sub_ids
        process_xls(sheet,data,False,self.env.user)
    

